package com.swx.identityservice.webapis;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swx.identityservice.dtos.dealrole.DealRoleListOutputDto;
import com.swx.identityservice.dtos.dealrole.DealRoleOutputDto;
import com.swx.identityservice.dtos.dealrole.InsertDealRoleDto;
import com.swx.identityservice.dtos.dealrole.UpdateDealRoleDto;
import com.swx.identityservice.models.ScbpDealRole;
import com.swx.identityservice.models.ScbpDept;
import com.swx.identityservice.service.ScbpDealRoleService;
import com.swx.identityservice.service.ScbpDeptService;
import com.swx.identityservice.utils.JaxrsResponse;
import com.swx.identityservice.utils.ResponseMessages;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lxh
 * @version V1.0.0
 * @date 2018-6-1 15:21
 */

@Slf4j
@Api(tags = "审核人员控制器")
@RestController
public class ScbpDealRoleController {
    @Autowired
    private ScbpDealRoleService scbpDealRoleService;
    private ModelMapper modelMapper;
    @Autowired
    private ScbpDeptService scbpDeptService;

    public ScbpDealRoleController() { modelMapper = new ModelMapper();}
    @ApiOperation("新增审核角色")
    @PostMapping(value = "/adddealrole")
    public JaxrsResponse post(@RequestBody InsertDealRoleDto InsertDealRoleDto) {
        ScbpDealRole scbpDealRole = modelMapper.map(InsertDealRoleDto, ScbpDealRole.class);
        scbpDealRole.setCreationtime(new Date());
        ScbpDept scbpDept = scbpDeptService.selectDeptById(InsertDealRoleDto.getUnitid());
        scbpDealRole.setUnit(scbpDept.getTitle());
        int i = scbpDealRoleService.insertSelective(scbpDealRole);
        if (i > 0) return JaxrsResponse.builder().status("success").data(ResponseMessages.INSERT_SUCCESS).build();
        return JaxrsResponse.builder().message("fail").data(ResponseMessages.INSERT_FAIL).build();
    }


    @ApiOperation("编辑审核角色")
    @PostMapping(value = "/editdealrole")
    public JaxrsResponse post(@RequestBody UpdateDealRoleDto updateDealRoleDto) {
        ScbpDealRole scbpDealRole = modelMapper.map(updateDealRoleDto, ScbpDealRole.class);
        scbpDealRole.setLastmodificationtime(new Date());
        ScbpDept scbpDept = scbpDeptService.selectDeptById(updateDealRoleDto.getUnitid());
        scbpDealRole.setUnit(scbpDept.getTitle());
        int i = scbpDealRoleService.updateByPrimaryKeySelective(scbpDealRole);
        if (i > 0)return JaxrsResponse.builder().status("success").data(ResponseMessages.UPDATE_SUCCESS).build();
        return JaxrsResponse.builder().message("fail").data(ResponseMessages.UPDATE_FAIL).build();
    }

    @ApiOperation("删除审核人员")
    @DeleteMapping(value = "/deldealrole")
    public JaxrsResponse delete(@RequestParam int id) {
        ScbpDealRole dealRole = new ScbpDealRole();
        dealRole.setId(id);
        dealRole.setIsdeleted(true);
        int i = scbpDealRoleService.updateByPrimaryKeySelective(dealRole);
        if (i > 0) return JaxrsResponse.builder().data(ResponseMessages.DELETE_SUCCESS).status("success").build();
        return JaxrsResponse.builder().data(ResponseMessages.DELETE_FAIL).message("fail").build();
    }

    @ApiOperation("查询审核人员")
    @GetMapping(value = "/getdealroles")
    public JaxrsResponse getdeal(@RequestParam(defaultValue = "1", value = "pageIndex") int pageIndex,
                                 @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
                                 String Name,
                                 String ShowIndexName,
                                 Integer Priority,
                                 String Unit,
                                 String NameCode,
                                 Boolean IsDecided) {

        PageHelper.startPage(pageIndex, pageSize);
        List<ScbpDealRole> scbpDealRoles = scbpDealRoleService.selectDealRoles(Name,ShowIndexName,Priority,Unit,NameCode,IsDecided);
        PageInfo<ScbpDealRole> pageInfo = new PageInfo<>(scbpDealRoles);
        DealRoleListOutputDto dealRoleListOutputDto = new DealRoleListOutputDto();
        dealRoleListOutputDto.setTotal(pageInfo.getTotal());
        pageInfo.getList().forEach(u -> {
            DealRoleOutputDto userOutputDto = modelMapper.map(u, DealRoleOutputDto.class);
            dealRoleListOutputDto.addUser(userOutputDto);
        });
        return JaxrsResponse.builder().code(0).status("success").data(dealRoleListOutputDto).build();
    }
}



